package com.cykj.pos.controller.sftdemo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.cykj.pos.util.RSASignUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author yangyuanyuan
 * @version 1.0
 * @date 2020/9/21 16:26
 */
public class SignTest {

    public static boolean getData(String privateKey,String requestJson) throws Exception {

        //生成签名
//        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ0s8MgeF8EkqziOGvpmgkDJf46nBPuPK+R5YEm9oSpzuJ/3O5U/PypAKNo+vbxebobMeCwej6UaVLrDdzKMoRuh5XnBmzQj/KORqgxFMAR6S5rhKtPFK+t2zzrrrErPYZFxleGVVKjuLMOzUOfedsveemPunX8VljKmD09zGu6BAgMBAAECgYA9mrq5RQo8erndjd1HHdyZtiCX8iDC1Kf4ghZacMf1kqvfjxC8e7CvR7x7ZjIJ3d1mWkgTytq0dLCXDzvuBS6zv6i8In6zNor8F9lDGX0ysxe/PUcKzhuT1725p3OuTM/pDNXWtz8I1DF37FXyaOHzZLKJgTQ1M6PbTSiDhL0R0QJBAMw9cpyRoc5O0aARVmgHCtY8rvg3PXnl89vlvdSthj/YBkwqe+xrCuMY6OGIpt4asRBSuM+PibZo8F099NqkohUCQQDFAhLurH2pmg+V/ojt2bFVa8rm4r5WIfCs7NfhJ6Gk+Oqas7nNkgtR0vvrPztsDNnt5dSF8BDmVj2AJEFg9HG9AkBxRA6gp98dI/b7F0JdFLK81xCN1UzBonZX2BQrs0QFeg8cBF+PYFm8Ud8s3HT2LcVOcSHO8iKER7QtcaOt8al1AkEAq19XerkfgtZaMn89O7H/ma/vy5J4QdK6WqKYU+q2qRxCcBphSVp+cEoZDBVwKlivhktD6NLg0I06aGqhWKaq/QJAIVDAiZeS9Lxo9cAFUgo6C76GVgcm3GYOxfotRNkoCAMoodsEHEGTFglxVeQDdTsKsCagpjaaQ6q36bJexs1Uyw==";
//        String requestJson = "{\"amt\":\"55.66\",\"fee\":\"0.32\",\"merchantId\":\"88038421\",\"orderId\":\"828800\",\"stlAmt\":\"55.34\",\"stlBillNo\":\"88038421210601144840269752964105\",\"stlType\":\"T0\",\"txnTime\":\"20210601144837\"}";
        System.out.println(privateKey);
        System.out.println(requestJson);
        JSONObject requestObj = JSON.parseObject(requestJson, Feature.OrderedField);
        Set<String> keys = requestObj.keySet();
        Map<String, String> requestMap = new HashMap<String, String>();
        for(String key : keys) {
            requestMap.put(key, requestObj.getString(key));
        }
        String mac = RSASignUtil.generateSign(requestMap, privateKey, Arrays.asList("mac","class"));
        System.out.println("mac=" + mac);
        //验证签名
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdLPDIHhfBJKs4jhr6ZoJAyX+OpwT7jyvkeWBJvaEqc7if9zuVPz8qQCjaPr28Xm6GzHgsHo+lGlS6w3cyjKEboeV5wZs0I/yjkaoMRTAEekua4SrTxSvrds8666xKz2GRcZXhlVSo7izDs1Dn3nbL3npj7p1/FZYypg9PcxrugQIDAQAB";
        Boolean verifyResult = RSASignUtil.verifySign(requestMap, publicKey, Arrays.asList("mac","class"), mac);
        System.out.println("verifyResult=" + verifyResult);
        return verifyResult;
    }
}
